<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('文章TDK配置')" />
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: jquery-smartwizard-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-set-add">
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">远程问诊</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">医药前沿</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-3" aria-expanded="false">每日药闻</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-4" aria-expanded="false">新药快讯</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-5" aria-expanded="false">相关政策</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-6" aria-expanded="false">行业动态</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-7" aria-expanded="false">安全用药</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-8" aria-expanded="false">用药常识</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-9" aria-expanded="false">用药误区</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-10" aria-expanded="false">健康护理</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-11" aria-expanded="false">健康饮食</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-12" aria-expanded="false">日常护理</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-13" aria-expanded="false">心理健康</a>
            </li>
            <li class=""><a data-toggle="tab" href="#tab-14" aria-expanded="false">临床招募</a>
            </li>
        </ul>
        <div class="tab-content">

        </div>
        <div style="text-align: center">
            <a class="btn btn-newadd" onclick="submitHandler()">
                保存
            </a>
<!--            <a class="btn btn-newadd" onclick="returnPage()">-->
<!--                返回-->
<!--            </a>-->
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: jquery-smartwizard-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: summernote-js"/>
<script src="/ueditor/ueditor.config.js" type="text/javascript" charSet="utf-8"></script>
<script src="/ueditor/ueditor.all.js" type="text/javascript" charSet="utf-8"></script>
<script src="/ueditor/lang/zh-cn/zh-cn.js" type="text/javascript" charSet="utf-8"></script><script th:inline="javascript">
    var prefix = ctx + "ydz/newsTdk"

    var typeList = [];
    $("#form-set-add").validate({
        focusCleanup: true
    });

    $.ajax({
        url: '/ydz/newsTdk/list',
        type: 'POST',
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.code == 0){
                typeList = data.rows;
            }else if (data.msg == 'false'){
                $.modal.alertError('获取配置数据失败，请联系管理员');
            }
        }
    })

    //富文本
    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action == 'uploadimage' || action == 'uploadscrawl') {
            return '/common/upload';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    }

    var html = "";
    var content1={}
    var content2={}
    var content3={}
    var content4={}
    var content5={}
    var content6={}
    var content7={}
    var content8={}
    var content9={}
    var content10={}
    var content11={}
    var content12={}
    var content13={}
    var content14={}
    typeList.forEach(function (item,index){
        var tab = "tab-"+item.id;
        var active = "";
        //默认选中第一个
        if(1==item.id){
            active="active"
        }
        var sid = "id"+item.id;
        var stitle = "title"+item.id;
        var skeyword = "keyword"+item.id;
        var sdescription = "description"+item.id;
        var sillustrate = "illustrate"+item.id;
        var scontent = "content"+item.id;

        var name = item.name;
        var title = item.title == null ? "":item.title;
        var keyword = item.keyword == null ? "":item.keyword;
        var description = item.description == null ? "":item.description;
        var illustrate = item.illustrate == null ? "":item.illustrate;
        var content = item.content == null ? "":item.content;
        html+= "<div id='"+tab+"' class='tab-pane "+active+"' style='padding: 20px'>"
            +"  <input id='"+sid+"' value='"+item.id+"' class='form-control' placeholder='请输入标题' type='hidden'>"
            +"<div class='form-group'>"
            +"<label class='col-sm-2 control-label'> "+name+"标题</label>"
            +"<div class='col-sm-10'>"
            +"  <input name='"+stitle+"' value='"+title+"' class='form-control' placeholder='请输入标题' type='text'>"
            +"   </div>"
            +" </div>"
            +"<div class='form-group'>"
            +"    <label class='col-sm-2 control-label'>"+name+"关键词</label>"
            +"    <div class='col-sm-10'>"
            +"        <input name='"+skeyword+"' value='"+keyword+"' class='form-control' placeholder='请输入关键词' type='text'>"
            +"    </div>"
            +"</div>"
            +"<div class='form-group'>"
            +"    <label class='col-sm-2 control-label'>"+name+"描述</label>"
            +"    <div class='col-sm-10'>"
            +"        <input name='"+sdescription+"' value='"+description+"'  class='form-control' placeholder='请输入描述' type='text'>"
            +"    </div>"
            +"</div>"
            +"<div class='form-group'>"
            +"    <label class='col-sm-2 control-label'>"+name+"说明</label>"
            +"    <div class='col-sm-10'>"
            +"        <input name='"+sillustrate+"' value='"+illustrate+"' class='form-control' placeholder='请输入说明' type='text'>"
            +"    </div>"
            +"</div>"
            +"<div class='form-group'>"
            +"    <label class='col-sm-2 control-label'>"+name+"内容</label>"
            +"    <div class='col-sm-10'>"
            +"        <input type='hidden' class='form-control' required>"
            +"            <script name='"+scontent+"' id='"+scontent+"' type='text/plain'  >"
            +"</"
            +"script>"
            +"</div>"
            +"</div>"
            +"</div>"

        scontent = UE.getEditor(scontent, {
            autoHeightEnabled: false,//是否自动长高
            autoFloatEnabled: true,//是否保持toolbar的位置不动
            scaleEnabled: true,//是否可以拉伸长高
            initialFrameWidth: 'auto',//初始化编辑器宽度
            initialFrameHeight: 300,//初始化编辑器高度
        });
        scontent.ready(function () {
            scontent.execCommand("insertHtml", content);//富文本回显
        })
        if(item.id=="1"){
            content1 = scontent;
        }else if(item.id=="2"){
            content2 = scontent;
        }else if(item.id=="3"){
            content3 = scontent;
        }else if(item.id=="4"){
            content4 = scontent;
        }else if(item.id=="5"){
            content5 = scontent;
        }else if(item.id=="6"){
            content6 = scontent;
        }else if(item.id=="7"){
            content7 = scontent;
        }else if(item.id=="8"){
            content8 = scontent;
        }else if(item.id=="9"){
            content9 = scontent;
        }else if(item.id=="10"){
            content10 = scontent;
        }else if(item.id=="11"){
            content11 = scontent;
        }else if(item.id=="12"){
            content12 = scontent;
        }else if(item.id=="13"){
            content13 = scontent;
        }else if(item.id=="14"){
            content14 = scontent;
        }

    })
    $(".tab-content").append(html);
    //
    // var content1 = UE.getEditor('content1', {
    //     autoHeightEnabled: false,//是否自动长高
    //     autoFloatEnabled: true,//是否保持toolbar的位置不动
    //     scaleEnabled: true,//是否可以拉伸长高
    //     initialFrameWidth: 'auto',//初始化编辑器宽度
    //     initialFrameHeight: 300,//初始化编辑器高度
    // });
    // content1.ready(function () {
    // })

    // var content2 = UE.getEditor('content2', {
    //     autoHeightEnabled: false,//是否自动长高
    //     autoFloatEnabled: true,//是否保持toolbar的位置不动
    //     scaleEnabled: true,//是否可以拉伸长高
    //     initialFrameWidth: 'auto',//初始化编辑器宽度
    //     initialFrameHeight: 300,//初始化编辑器高度
    // });
    // content2.ready(function () {
    // })
    //
    // var content3 = UE.getEditor('content3', {
    //     autoHeightEnabled: false,//是否自动长高
    //     autoFloatEnabled: true,//是否保持toolbar的位置不动
    //     scaleEnabled: true,//是否可以拉伸长高
    //     initialFrameWidth: 'auto',//初始化编辑器宽度
    //     initialFrameHeight: 300,//初始化编辑器高度
    // });
    // content3.ready(function () {
    // })
    //
    // var content4 = UE.getEditor('content4', {
    //     autoHeightEnabled: false,//是否自动长高
    //     autoFloatEnabled: true,//是否保持toolbar的位置不动
    //     scaleEnabled: true,//是否可以拉伸长高
    //     initialFrameWidth: 'auto',//初始化编辑器宽度
    //     initialFrameHeight: 300,//初始化编辑器高度
    // });
    // content4.ready(function () {
    // })
    //
    // var content5 = UE.getEditor('content5', {
    //     autoHeightEnabled: false,//是否自动长高
    //     autoFloatEnabled: true,//是否保持toolbar的位置不动
    //     scaleEnabled: true,//是否可以拉伸长高
    //     initialFrameWidth: 'auto',//初始化编辑器宽度
    //     initialFrameHeight: 300,//初始化编辑器高度
    // });
    // content5.ready(function () {
    // })

    function submitHandler() {
        var list = [];
        typeList.forEach(function (item,index){
            var data = {}
            data.id = $('#id'+item.id).val();
            data.title = $('input[name=' + 'title' + item.id + ']').val();
            data.keyword = $('input[name=' + 'keyword' + item.id + ']').val();
            data.description = $('input[name=' + 'description' + item.id + ']').val();
            data.illustrate = $('input[name=' + 'illustrate' + item.id + ']').val();
             var str = "content" + item.id;
             var a = eval(str)
             data.content = a.getContent();
            list.push(data);
        })
        console.log("list",list)
        var value = JSON.stringify(list)
        if ($.validate.form()) {
            saveSet(prefix + "/editNewsSet", value);
        }
    }
    function saveSet(url, data, callback) {
        var config = {
            url: url,
            type: "post",
            dataType: "json",
            data: data,
            headers: {
                'Content-Type': 'application/json;charset=utf8'
            },
            success: function (res) {
                if(res.code==="1"){
                    $.modal.msgSuccess(res.msg);
                }else{
                    $.modal.alertError(res.msg);
                }
            }
        };
        $.ajax(config)
    }
    function returnPage() {
        window.close();
    }
</script>
</body>
</html>